package com.example.littleSpider.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.example.littleSpider.myapplication.R;
import com.example.littleSpider.myapplication.bean.ShoppingCartBean;
import com.example.littleSpider.myapplication.myInterfaces.CheckAllListener;
import com.littlespider.mylibrary.util.Print;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by littleSpider on 2018/1/2.
 */

public class ShoppingCartAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private CheckAllListener checkAllListener;
    private List<ShoppingCartBean> groupList;
    private List<List<ShoppingCartBean>> childList;

    public ShoppingCartAdapter(Context mContext, CheckAllListener checkAllListener, List<ShoppingCartBean> groupList, List<List<ShoppingCartBean>> childList) {
        this.mContext = mContext;
        this.checkAllListener = checkAllListener;
        this.groupList = groupList;
        this.childList = childList;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_parent_shopping_cart, null);
        final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.cb_check_item);
        final boolean currentChecked = groupList.get(groupPosition).isChoose();
        checkBox.setChecked(currentChecked);
                ShoppingCartBean bean = new ShoppingCartBean();
                bean.setChoose(currentChecked);
                groupList.set(groupPosition, bean);
                List<Boolean> booleanList = new ArrayList<Boolean>();
                for (int i = 0; i < groupList.size(); i++) {
//                    Print.i("checkBox Status:" + groupList.get(i).isChoose());
                    booleanList.add(groupList.get(i).isChoose());
                }
                if (booleanList.indexOf(false) == -1){
                    Print.i("此处应该全选");
                    checkAllListener.selectorAll();
                }else {
                    Print.i("此处应该全不选");
                    checkAllListener.selectorNotAll();
                }
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShoppingCartBean groupBean = new ShoppingCartBean();
                groupBean.setChoose(!currentChecked);
                groupList.set(groupPosition, groupBean);
                for (int i = 0; i < childList.get(groupPosition).size(); i++) {
                    ShoppingCartBean bean = new ShoppingCartBean();
                    bean.setChoose(!currentChecked);
                    childList.get(groupPosition).set(i, bean);
                }
                notifyDataSetChanged();
            }
        });
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Print.i("please change me.");
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_shopping_cart, null);
        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
        checkBox.setChecked(childList.get(groupPosition).get(childPosition).isChoose());
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                ShoppingCartBean bean = new ShoppingCartBean();
                bean.setChoose(isChecked);
                childList.get(groupPosition).set(childPosition, bean);
                List<Boolean> booleanList = new ArrayList<Boolean>();
                for (int i = 0; i < childList.get(groupPosition).size(); i++) {
                    booleanList.add(childList.get(groupPosition).get(i).isChoose());
                }
                ShoppingCartBean shoppingCartBean = new ShoppingCartBean();
                if (booleanList.indexOf(false) == -1){
                    shoppingCartBean.setChoose(true);
                }else {
                    shoppingCartBean.setChoose(false);
                }
                groupList.set(groupPosition, shoppingCartBean);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

}
